// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2022 Huawei Technologies Co., Ltd */
#ifndef HIUDK_SDK_CQM_ADPT_H
#define HIUDK_SDK_CQM_ADPT_H

#if defined(HIUDK_SDK_ADPT) && defined(HIUDK_SDK)

#include "hinic3_cqm.h"
static inline s32 sdk_cqm_bloomfilter_dec(void *udkdev, u16 func_id, u64 id)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_bloomfilter_dec(sdk_hwdev, func_id, id);
}

static inline s32 sdk_cqm_bloomfilter_inc(void *udkdev, u16 func_id, u64 id)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_bloomfilter_inc(sdk_hwdev, func_id, id);
}

static inline cqm_cmd_buf_s *sdk_cqm_cmd_alloc(void *udkdev)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_cmd_alloc(sdk_hwdev);
}

static inline void sdk_cqm_cmd_free(void *udkdev, cqm_cmd_buf_s *cmd_buf)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_cmd_free(sdk_hwdev, cmd_buf);
}

static inline s32 sdk_cqm_fake_vf_num_set(void *udkdev, u16 fake_vf_num_cfg)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_fake_vf_num_set(sdk_hwdev, fake_vf_num_cfg);
}

static inline void sdk_cqm_function_hash_buf_clear(void *udkdev, s32 global_funcid)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_function_hash_buf_clear(sdk_hwdev, global_funcid);
}

static inline void sdk_cqm_function_timer_clear(void *udkdev, u32 function_id)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_function_timer_clear(sdk_hwdev, function_id);
}

static inline void *sdk_cqm_get_db_addr(void *udkdev, u32 service_type)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_get_db_addr(sdk_hwdev, service_type);
}

static inline s32 sdk_cqm_get_hardware_db_addr(void *udkdev, u64 *addr,
					       enum hinic3_service_type service_type)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_get_hardware_db_addr(sdk_hwdev, addr, service_type);
}

static inline s32 sdk_cqm_lb_send_cmd_box(void *udkdev, u8 mod, u8 cmd, u8 cos_id,
					  cqm_cmd_buf_s *buf_in, cqm_cmd_buf_s *buf_out,
             				  u64 *out_param, u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_lb_send_cmd_box(sdk_hwdev, mod, cmd, cos_id, buf_in, buf_out,
				    out_param, timeout, channel);
}

static inline cqm_queue_s *sdk_cqm_object_fc_srq_create(void *udkdev, u32 service_type,
							cqm_object_type_e object_type, u32 wqe_number,
							u32 wqe_size, void *object_priv)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_fc_srq_create(sdk_hwdev, service_type, object_type, wqe_number,
					 wqe_size, object_priv);
}

static inline cqm_object_s *sdk_cqm_object_get(void *udkdev, cqm_object_type_e object_type,
					       u32 index, bool bh)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_get(sdk_hwdev, object_type, index, bh);
}

static inline cqm_queue_s *sdk_cqm_object_nonrdma_queue_create(void *udkdev, u32 service_type,
							       cqm_object_type_e object_type,
							       u32 wqe_number, u32 wqe_size,
							       void *object_priv)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_nonrdma_queue_create(sdk_hwdev, service_type, object_type, wqe_number,
						wqe_size, object_priv);
}

static inline cqm_qpc_mpt_s *sdk_cqm_object_qpc_mpt_create(void *udkdev, u32 service_type,
							   cqm_object_type_e object_type, u32 object_size,
							   void *object_priv, u32 index,
							   bool low2bit_align_en)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_qpc_mpt_create(sdk_hwdev, service_type, object_type, object_size,
					  object_priv, index, low2bit_align_en);
}

static inline cqm_queue_s *sdk_cqm_object_rdma_queue_create(void *udkdev, u32 service_type,
							    cqm_object_type_e object_type, u32 object_size,
							    void *object_priv, bool room_header_alloc, u32 xid)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_rdma_queue_create(sdk_hwdev, service_type, object_type, object_size,
					     object_priv, room_header_alloc, xid);
}

static inline cqm_mtt_rdmarc_s *sdk_cqm_object_rdma_table_get(void *udkdev, u32 service_type,
							      cqm_object_type_e object_type,
							      u32 index_base, u32 index_number)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_rdma_table_get(sdk_hwdev, service_type, object_type, index_base,
					  index_number);
}

static inline cqm_queue_s *sdk_cqm_object_recv_queue_create(void *udkdev, u32 service_type,
							    cqm_object_type_e object_type, u32 init_rq_num,
							    u32 container_size, u32 wqe_size, void *object_priv)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_recv_queue_create(sdk_hwdev, service_type, object_type, init_rq_num,
					     container_size, wqe_size, object_priv);
}

static inline cqm_queue_s *sdk_cqm_object_share_recv_queue_create(void *udkdev, u32 service_type,
								  cqm_object_type_e object_type,
								  u32 container_number,
								  u32 container_size, u32 wqe_size)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_object_share_recv_queue_create(sdk_hwdev, service_type, object_type,
						   container_number, container_size, wqe_size);
}

static inline s32 sdk_cqm_ring_direct_wqe_db(void *udkdev, u32 service_type, u8 db_count,
					     void *direct_wqe)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_ring_direct_wqe_db(sdk_hwdev, service_type, db_count, direct_wqe);
}

static inline s32 sdk_cqm_ring_hardware_db(void *udkdev, u32 service_type, u8 db_count, u64 db)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_ring_hardware_db(sdk_hwdev, service_type, db_count, db);
}

static inline s32 sdk_cqm_send_cmd_box(void *udkdev, u8 mod, u8 cmd, cqm_cmd_buf_s *buf_in,
				       cqm_cmd_buf_s *buf_out, u64 *out_param, u32 timeout,
				       u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	 }

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_send_cmd_box(sdk_hwdev, mod, cmd, buf_in, buf_out, out_param, timeout, channel);
}

static inline s32 sdk_cqm_send_cmd_imm(void *udkdev, u8 mod, u8 cmd, cqm_cmd_buf_s *buf_in,
				       u64 *out_param, u32 timeout, u16 channel)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_send_cmd_imm(sdk_hwdev, mod, cmd, buf_in, out_param, timeout, channel);
}

static inline s32 sdk_cqm_service_register(void *udkdev, service_register_template_s *service_template)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_service_register(sdk_hwdev, service_template);
}

static inline void sdk_cqm_service_unregister(void *udkdev, u32 service_type)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_service_unregister(sdk_hwdev, service_type);
}

static inline void *sdk_cqm_timer_base(void *udkdev)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_timer_base(sdk_hwdev);
}

static inline s32 sdk_cqm_dtoe_share_recv_queue_create(void *udkdev, u32 csize,
                        				 u32 *cnt, u32 *idx)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return -EINVAL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_dtoe_share_recv_queue_create(sdk_hwdev, csize, cnt, idx);
}


static inline void sdk_cqm_dtoe_free_srq_bitmap_index(void *udkdev, u32 count, u32 index)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_dtoe_free_srq_bitmap_index(sdk_hwdev, count, index);
}

static inline bool sdk_cqm_need_secure_mem(void *udkdev)
{
	void *sdk_hwdev = NULL;

	if (!CHECK_UDK_DEV(udkdev)) {
		pr_err("udkdev is error! \n");
		return NULL;
	}

	sdk_hwdev = GET_SDKHWDEV_FROM_HIUDKHWDEV(udkdev);
	return cqm3_need_secure_mem(sdk_hwdev);
}

#define cqm_bloomfilter_dec sdk_cqm_bloomfilter_dec
#define cqm_bloomfilter_inc sdk_cqm_bloomfilter_inc
#define cqm_cmd_alloc sdk_cqm_cmd_alloc
#define cqm_cmd_free sdk_cqm_cmd_free
#define cqm_fake_vf_num_set sdk_cqm_fake_vf_num_set
#define cqm_function_hash_buf_clear sdk_cqm_function_hash_buf_clear
#define cqm_function_timer_clear sdk_cqm_function_timer_clear
#define cqm_get_db_addr sdk_cqm_get_db_addr
#define cqm_get_hardware_db_addr sdk_cqm_get_hardware_db_addr
#define cqm_lb_send_cmd_box sdk_cqm_lb_send_cmd_box
#define cqm_object_fc_srq_create sdk_cqm_object_fc_srq_create
#define cqm_object_get sdk_cqm_object_get
#define cqm_object_nonrdma_queue_create sdk_cqm_object_nonrdma_queue_create
#define cqm_object_qpc_mpt_create sdk_cqm_object_qpc_mpt_create
#define cqm_object_rdma_queue_create sdk_cqm_object_rdma_queue_create
#define cqm_object_rdma_table_get sdk_cqm_object_rdma_table_get
#define cqm_object_recv_queue_create sdk_cqm_object_recv_queue_create
#define cqm_object_share_recv_queue_create sdk_cqm_object_share_recv_queue_create
#define cqm_ring_direct_wqe_db sdk_cqm_ring_direct_wqe_db
#define cqm_ring_hardware_db sdk_cqm_ring_hardware_db
#define cqm_send_cmd_box sdk_cqm_send_cmd_box
#define cqm_send_cmd_imm sdk_cqm_send_cmd_imm
#define cqm_service_register sdk_cqm_service_register
#define cqm_service_unregister sdk_cqm_service_unregister
#define cqm_timer_base sdk_cqm_timer_base
#define cqm_need_secure_mem sdk_cqm_need_secure_mem

#define cqm_object_resize_alloc_new cqm3_object_resize_alloc_new
#define cqm_object_resize_free_new cqm3_object_resize_free_new
#define cqm_object_resize_free_old cqm3_object_resize_free_old
#define cqm_object_share_recv_queue_add_container cqm3_object_share_recv_queue_add_container
#define cqm_object_srq_add_container_free cqm3_object_srq_add_container_free
#define cqm_ring_software_db cqm3_ring_software_db
#define cqm_object_delete cqm3_object_delete
#define cqm_srq_used_rq_container_delete cqm3_srq_used_rq_container_delete
#define cqm_object_offset_addr cqm3_object_offset_addr
#define cqm_object_put cqm3_object_put
#define cqm_object_funcid cqm3_object_funcid

#define  hiudk_cqm_object_delete(x, y) cqm3_object_delete(y)
#define  hiudk_cqm_object_funcid(x, y) cqm3_object_funcid(y)
#define  hiudk_cqm_object_offset_addr(x, y, z, m) cqm3_object_offset_addr(y, z, m)
#define  hiudk_cqm_object_put(x, y) cqm3_object_put(y)
#define  hiudk_cqm_object_resize_alloc_new(x, y, z) cqm3_object_resize_alloc_new(y, z)
#define  hiudk_cqm_object_resize_free_new(x, y) cqm3_object_resize_free_new(y)
#define  hiudk_cqm_object_resize_free_old(x, y) cqm3_object_resize_free_old(y)
#define  hiudk_cqm_object_share_recv_queue_add_container(x, y) cqm3_object_share_recv_queue_add_container(y)
#define  hiudk_cqm_object_srq_add_container_free(x, y, z) cqm3_object_srq_add_container_free(y, z)
#define  hiudk_cqm_ring_software_db(x, y, z) cqm3_ring_software_db(y, z)
#define  hiudk_cqm_srq_used_rq_container_delete(x, y, z) cqm3_srq_used_rq_container_delete(y, z)

#endif
#endif
